package com.twb.auth.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.twb.auth.entity.twbProject.TwbProject;
import com.twb.auth.entity.twbProject.dto.GetTwbProjectByIdDto;
import com.twb.auth.entity.twbProject.dto.ListTwbProjectDto;
import com.twb.auth.entity.twbProject.dto.SearchTwbProjectDto;
import com.twb.auth.entity.twbProject.vo.ListTwbProjectVo;
import com.twb.auth.entity.twbProject.vo.SaveTwbProjectVo;
import com.twb.auth.entity.twbProject.vo.SearchTwbProjectVo;
import com.twb.auth.entity.twbProject.vo.UpdateTwbProjectByIdVo;
import com.twb.auth.service.ITwbProjectService;
import com.twb.core.base.Result;
import com.twb.core.base.controller.BaseController;
import com.twb.core.base.entity.BaseLoginUser;
import com.twb.core.base.entity.TwbPageInfo;
import com.twb.core.security.annotation.Auth;
import com.twb.core.util.BeanUtils;
import com.twb.core.util.Results;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @ClassName TwbProjectController
 * @Description 项目管理
 * @author DSY
 * @date 2018年11月13日 下午5:12:32
 */
@RestController
@RequestMapping("/sys/twbProject")
@Api(value = "TwbProjectController", tags = "项目管理接口")
public class TwbProjectController extends BaseController {

	@Autowired
	ITwbProjectService twbProjectService;

	/**
	 * @Title  searchTwbProject
	 * @Description 项目分页查询
	 * @param vo
	 * @param pageVo
	 * @return
	 * @author DSY
	 * @date 2018年11月13日 下午5:27:53
	 */
	@GetMapping("/search")
	@ApiOperation(value = "项目分页查询")
	public Result<TwbPageInfo<SearchTwbProjectDto>> searchTwbProject(SearchTwbProjectVo vo) {
		TwbPageInfo<SearchTwbProjectDto> result = twbProjectService.searchTwbProject(vo);
		return Results.successWithData(result);
	}

	/**
	 * @Title  getTwbProjectById
	 * @Description 根据ID获取项目详情
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2018年11月14日 下午3:48:06
	 */
	@GetMapping("/getById")
	@ApiOperation(value = "根据ID获取项目详情")
	public Result<GetTwbProjectByIdDto> getTwbProjectById(
			@NotBlank @RequestParam @ApiParam(value = "序号", name = "id", required = true) String id) {
		TwbProject twbProject = twbProjectService.getById(id);
		GetTwbProjectByIdDto dto = new GetTwbProjectByIdDto();
		BeanUtils.copyNotNullProperties(twbProject, dto);
		return Results.successWithData(dto);
	}

	/**
	 * @Title  saveTwbProject
	 * @Description 保存项目信息
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2018年11月14日 下午3:52:55
	 */
	@PostMapping("/save")
	@ApiOperation(value = "保存项目信息")
	public Result<String> saveTwbProject(@RequestBody @Valid SaveTwbProjectVo vo) {
		TwbProject record = new TwbProject();
		record.setAppToken(vo.getCode().toUpperCase());
		record.setIsdel(0);
		long count = twbProjectService.count(record);
		if (count > 0) {
			return Results.failure("项目编号[" + vo.getCode() + "]已存在");
		}
		boolean result = twbProjectService.saveTwbProject(vo);
		if (result) {
			return Results.success("保存成功");
		} else {
			return Results.failure("保存失败");
		}
	}

	/**
	 * @Title  updateTwbProjectById
	 * @Description 更新项目管理信息
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2018年11月14日 下午4:08:15
	 */
	@PutMapping("/updateById")
	@ApiOperation(value = "更新项目信息")
	public Result<String> updateTwbProjectById(@RequestBody @Valid UpdateTwbProjectByIdVo vo) {
		boolean result = twbProjectService.updateTwbProjectById(vo);
		if (result) {
			return Results.success("保存成功");
		} else {
			return Results.failure("保存失败");
		}
	}

	/**
	 * @Title  updateTwbSyscofigStatusById
	 * @Description 更新项目状态
	 * @param id
	 * @param status
	 * @return
	 * @author DSY
	 * @date 2018年11月29日 下午5:56:58
	 */
	@PutMapping("/updateStatus")
	@ApiOperation(value = "更新项目状态")
	public Result<String> updateTwbSyscofigStatusById(
			@NotBlank @RequestParam @ApiParam(value = "序号", name = "id", required = true) String id,
			@NotBlank @RequestParam @ApiParam(value = "状态(0,失效;1,生效)", name = "status", required = true) String status) {
		TwbProject twbProject = twbProjectService.getById(id);
		if (null == twbProject) {
			return Results.failure("项目信息不存在");
		}
		TwbProject record = new TwbProject();
		record.setId(id);
		record.setStatus(status);
		twbProjectService.setUpdateInfo(record);

		boolean result = twbProjectService.updateById(record);
		if (result) {
			return Results.success("操作成功");
		} else {
			return Results.failure("操作失败");
		}
	}

	/**
	 * @Title  deleteTwbProjectById
	 * @Description 删除项目信息
	 * @param id
	 * @return
	 * @author DSY
	 * @date 2018年11月14日 下午4:21:46
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "删除项目信息")
	public Result<String> deleteTwbProjectById(
			@NotBlank @RequestParam @ApiParam(value = "序号", name = "id", required = true) String id) {
		boolean result = twbProjectService.deleteTwbProjectById(id);
		if (result) {
			return Results.success("删除成功");
		} else {
			return Results.failure("删除失败");
		}
	}

	/**
	 * @Title  listTwbProject
	 * @Class: TwbProjectController.java
	 * @Description 项目列表查询（不分页）
	 * @param vo
	 * @return
	 * @author DSY
	 * @date 2019年12月28日 下午9:41:38
	 */
	@Auth(verifyURL = false)
	@GetMapping("/list")
	@ApiOperation(value = "项目列表查询（不分页）")
	public Result<List<ListTwbProjectDto>> listTwbProject(ListTwbProjectVo vo) {
		vo.setStatus("1");
		List<ListTwbProjectDto> list = twbProjectService.listTwbProject(vo);
		return Results.successWithData(list);
	}

	/**
	 * @Title projectsByAppIds
	 * @Description 根据appIds查询项目信息
	 * @param appIds
	 * @return
	 * @author DSY
	 * @date 2020年6月18日 上午10:36:22
	 */
	@Auth(verifyURL = false)
	@GetMapping("/projectsByAppIds")
	@ApiOperation(value = "项目列表查询（不分页）")
	public Result<List<GetTwbProjectByIdDto>> projectsByAppIds(String appIds) {
		BaseLoginUser loginUser = this.getLoginUser();
		List<TwbProject> redisProject = twbProjectService.getRedisProject();

		List<GetTwbProjectByIdDto> rtn = new ArrayList<GetTwbProjectByIdDto>();

		if (loginUser.isDeveloper()) {
			
			for (TwbProject twbProject : redisProject) {
				if (twbProject != null && "1".equals(twbProject.getAppType())) {
					GetTwbProjectByIdDto projectByIdDto = new GetTwbProjectByIdDto();

					BeanUtils.copyNotNullProperties(twbProject, projectByIdDto);
					
					rtn.add(projectByIdDto);
				}
			}
		} else {
			
			for (String appId : appIds.split(",")) {
				for (TwbProject twbProject : redisProject) {

					if (twbProject != null && "1".equals(twbProject.getAppType())) {

						if (appId.equals(twbProject.getAppId())) {
							GetTwbProjectByIdDto projectByIdDto = new GetTwbProjectByIdDto();

							BeanUtils.copyNotNullProperties(twbProject, projectByIdDto);

							rtn.add(projectByIdDto);
							
							break;
						}
					}
				}

			}
		}

		return Results.successWithData(rtn);
	}
}
